import java.io.*;
import java.net.*;
import java.util.Date;

import javax.activation.DataHandler;

public class myserver {

	public static void main(String args[]) throws IOException {
		//Initialize the value
		ServerSocket Server = null;
		String line;
		DataInputStream in;
		PrintStream out;
		Socket clientSocket = null;
		//Set the server's port number
		int port = Integer.parseInt(args[0]);
		
		try {
			Server = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println();
		}

		while (true) {
			//Wait for the client connect
			clientSocket = Server.accept();
			
			//Get the InputStream from the client and send the OutputStream to the client
			in = new DataInputStream(clientSocket.getInputStream());
			out = new PrintStream(clientSocket.getOutputStream());
			
			boolean end = false;
			while (!end) {
				line = in.readLine();
				// Command determination of the Receiving Command
				if (line.matches("GET /.+HTTP.+")) {
					try {
						//Language Processing
						String[] temp = line.split(" ");
						temp[1] = temp[1].replace("/", "");
						
						//Get the target File
						BufferedReader buf = new BufferedReader(
								new FileReader(
										"C://myserver/"
												+ temp[1]));
						
						File myfile = new File("C://myserver/"
								+ temp[1]);
						//Header Returning
						Date date = new Date();
						out.println("HTTP/1.1 200 OK");
						out.println("Servername: myServer");
						out.println("Date: " + date.toString());
						out.println("Content-Length: " + myfile.length());
						String[] temp2 = temp[1].split(".");
						if(temp[1].equals("html")||temp[1].equals("HTML"))
							out.println("Content-Type: text/html");
						else
							out.println("Content-Type: " + temp[1]);
						out.println("");
						
						//Send the File's content text
						String text = buf.readLine();

						while (text != null) {
							out.println(text);
							text = buf.readLine();
						}

					} catch (FileNotFoundException e2) {
						//Send the message if the required file not found
						out.println("404 NOT FOUND");
						
					}
					in.close();
					out.close();
					clientSocket.close();
					end = true;
				} else if (line.matches("PUT .+")) {
					//Receive File from the client Part
					String[] temp = line.split(" ");
					InputStream input = clientSocket.getInputStream();
					//Create the File
					FileOutputStream fos = new FileOutputStream(
							"C://myserver/" + temp[1]);
					BufferedOutputStream bos = new BufferedOutputStream(fos);
					
					//Create a byte array for reading purpose
					byte[] buffer = new byte[1024];
					int flag;
					//if the input stream bytes = 1024, write it to the file and keep listening. 
					//While the Stream is lesser then 1024, write to file then stop.
					do {
					flag = input.read(buffer);	
					bos.write(buffer, 0,flag);
					bos.flush();
					} while (flag == 1024);
				
					
					bos.close();
					fos.close();

					
					//Send the confirm message to the client then close the connection with client
					out.println("200 OK File Created");
					
					out.flush();

					clientSocket.close();
					end = true;

				}

			}
		}
	
	}

}
